package com.b2.controller.web;

import com.b2.context.BaseContext;
import com.b2.mapper.WebUserMapper;
import com.b2.pojo.dto.CourseRecommendDto;
import com.b2.pojo.entity.Course;
import com.b2.pojo.entity.WebUser;
import com.b2.result.Result;
import com.b2.service.CourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/web/Course")
@Api(tags = "课程相关接口")
@Slf4j
@CrossOrigin
public class CourseController {

    @Resource
    private CourseService courseService;
    @Autowired
    private WebUserMapper webUserMapper;

    @PostMapping("/list")
    @ApiOperation("查询所有课程")
    public Result<List<Course>> List(){
        Long userId = BaseContext.getCurrentId();
        WebUser webUser = webUserMapper.selectById(userId);
        // 如果是用户只查用户绑定课程
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        if (webUser.getUserRole().equals(0)){
            queryWrapper.eq("userId",userId);
        }
        List<Course> list = courseService.list(queryWrapper);
        return Result.success(list);
    }

    @PostMapping("/select")
    @ApiOperation("根据课程分类id和课程权限查询课程")
    public Result<List<Course>> select(@ApiParam("课程类型id") @RequestParam(name = "categoryId") Long categoryId,
                                           @ApiParam("课程权限") @RequestParam(name = "courseAuthority") String courseAuthority) {
        return courseService.select(categoryId, courseAuthority);
    }

    @PostMapping("/update")
    @ApiOperation("修改课程信息")
    public Result<String> update(@RequestBody Course course) {
         courseService.update(course);
         return Result.success();
    }

    /**
     * 添加课程
     *
     * @param course 课程对象
     * @return 返回响应结果
     */
    @ApiOperation("添加课程")
    @PostMapping("/add")
    public Result<List<Course>> add(@RequestBody Course course) {
        return courseService.add(course);
    }

    /**
     * 删除课程
     *
     * @param courseId 课程id
     * @return 返回响应结果
     */
    @ApiOperation("删除课程")
    @DeleteMapping("/delete")
    public Result<List<Course>> delete(@ApiParam("课程id") @RequestParam(name = "courseId") Long courseId) {
        return courseService.delete(courseId);
    }

    @ApiOperation("设置课程推荐")
    @PostMapping("/setRecommend")
    public Result<String> setRecommend(@ApiParam("课程id") @RequestParam(name = "courseId") Long courseId,
                                       @ApiParam("0 未推荐 1 热门课程 2 轮播图 3 热门课程和轮播图") @RequestParam(name = "courseRecommend") Integer courseRecommend) {
         return courseService.setRecommend(courseId, courseRecommend);
    }

    @ApiOperation("查询推荐课程")
    @GetMapping("/recommendList")
    public Result<List<CourseRecommendDto>> recommendList(@ApiParam("null 所有推荐课程 1 热门课程 2 轮播图 3 热门课程和轮播图(默认查询）")
                                                 @RequestParam(name = "courseRecommend", required = false)  Integer courseRecommend,
                                                 @ApiParam("课程权限") @RequestParam("courseAuthority") String courseAuthority) {
        return courseService.recommendList(courseRecommend, courseAuthority);
    }
}
